import json
import os
import  pandas as pd
from datetime import datetime, timedelta
import tos
import subprocess
import shutil

# Access key 和 Secret key 可在用户火山引擎账号中查找
ak = "AKLTMmFmMzVmNmY5ZDEyNDNmNWEyZTU2MDM3Y2EzMDJlZTk"
sk = "WVdNeFpXRXdNekV4Tm1ZME5EZGxPRGc0TUdFNE1HWXdNV1ZqTkdNNU9HRQ=="
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例，your endpoint 填写 tos-cn-beijing.volces.com，your region 填写 cn-beijing。
endpoint = "tos-cn-beijing.ivolces.com"
region = "cn-beijing"


#文件解密流程
def data_jiemi(file_list, vechicle_id, daystr, hourstr):
    for file in file_list:
        # /data/code/input/v2305175911/20231225/19/fcm4mhvrc77u0nv1hhbqg/asw/StUpload/20231225-193438/20231225-193438-aggregator-3307.bag
        print("file: "+file)
        bag_file_name = os.path.basename(file)
        baglist = bag_file_name.split("-")
        bagid = baglist[len(baglist) - 1].replace(".bag", "")
        bagtype = baglist[2]

        # /data/code/input/v2305175911/20231225/19/fcm4mhvrc77u0nv1hhbqg/
        # /data/code/input/v2305175911/20231225/19/fcm4mhvrc77u0nv1hhbqg/3308/
        # /data/code/input/v2305175911/20231225/19/fcm4mhvrc77u0nv1hhbqg/3308/aggregator/
        outputfile=file.split("/asw")[0]+"/source_json/"+bagid+"/"+bagtype+"/"
        print("outputfile: " + outputfile)
        decrypter(file, outputfile, vechicle_id, daystr, hourstr)


#文件解密：
def decrypter(zipfile, outputfile,vechicle_id,daystr,hourstr):
    path_exsist(outputfile)
    commands="export LD_LIBRARY_PATH=/data/code/parser_bag/libs/:$LD_LIBRARY_PATH; cd /data/code/parser_bag;  ./parser_bag  "+zipfile+"  "+outputfile+";"
    print("command: "+commands)
    result2 = subprocess.run(commands, shell=True, capture_output=True, text=True)
    print(result2)
#列举文件：
def list_files_in_directory(directory):
    file_list = []
    for root, dirs, files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_list.append(filepath)
    return file_list

#对文件进行解压：
def unzip(localpath):
    directory_path = os.path.dirname(localpath)
    print("directory_path: "+directory_path)
    commands = "cd "+directory_path+"; unzip " +localpath
    print("unzip 指令为： "+commands)
    result2 = subprocess.run(commands, shell=True, capture_output=True, text=True)
    print(result2)
#判断文件是否存在
def path_exsist(folder_path):
    if not os.path.exists(folder_path):
        # 如果文件夹不存在，创建它
        os.makedirs(folder_path)
        print("创建成功！")
    else:
        print("目录已经存在。")

#下载数据
def download(bucket_name,object_key,file_name,localpath):
    print("判断文件是否存在，不存在则创建。。")
    path_exsist(localpath)
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)
        # 若 file_name 为目录则将对象下载到此目录下, 文件名为对象名
        client.get_object_to_file(bucket_name, object_key, file_name)
    except tos.exceptions.TosClientError as e:
        # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
    except tos.exceptions.TosServerError as e:
        # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
        print('fail with server error, code: {}'.format(e.code))
        # request id 可定位具体问题，强烈建议日志中保存
        print('error with request id: {}'.format(e.request_id))
        print('error with message: {}'.format(e.message))
        print('error with http code: {}'.format(e.status_code))
    except Exception as e:
        print('fail with unknown error: {}'.format(e))
#上传数据：
def upload(bucket_name,object_key,file_name):
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)
        # 将本地文件上传到目标桶中
        # file_name为本地文件的完整路径。
        client.put_object_from_file(bucket_name, object_key, file_name)
    except tos.exceptions.TosClientError as e:
        # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
    except tos.exceptions.TosServerError as e:
        # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
        print('fail with server error, code: {}'.format(e.code))
        # request id 可定位具体问题，强烈建议日志中保存
        print('error with request id: {}'.format(e.request_id))
        print('error with message: {}'.format(e.message))
        print('error with http code: {}'.format(e.status_code))
    except Exception as e:
        print('fail with unknown error: {}'.format(e))

#时间戳转时间字符串：
def get_time_str(secs):
    # 使用datetime.fromtimestamp()将时间戳转换为datetime对象
    dt_object = datetime.fromtimestamp(secs)
    # 使用strftime()方法将datetime对象格式化为字符串
    time_string = dt_object.strftime('%Y-%m-%d %H:%M:%S')
    return time_string
# 读取json文件以字符串形势读取
def readJsonFileToStr(source_dir):
    data_list=[]
    json_list = []
    with open(source_dir, 'r') as file:

        for line in file:
            # 用 json.loads 解析每一行的 JSON 对象
            json_str=line.replace(" ", "").replace("\n", "")

            # and json_str == '}{':

            if json_str is not None and json_str != '}{':
                # print(json_str)
                json_list.append(json_str)
            elif json_str is not None and json_str == '}{':
                json_list.append("}")
                data_list.append(json_list)
                json_list = []
                json_list.append("{")
        data_list.append(json_list)
        return data_list

# write_2_local(source_100ms_dir, upload_100ms_file,object_key)
def write_2_local(source_dir,local_dir,object_key):
    data_list =readJsonFileToStr(source_dir)
    with open(local_dir, 'w',encoding="utf-8") as file:
        for list1 in data_list:
            contnet=""
            for str_js in list1:
                contnet+=str_js
            data = json.loads(contnet)
            data['path'] =object_key
            json_string = json.dumps(data, ensure_ascii=False)
            file.write(json_string)
            file.write('\n')
def read_can_in(upload_can_dir, dict_can_dir):
    with open(upload_can_dir, 'r', encoding="utf-8") as file:
        with open(dict_can_dir, 'w',encoding="utf-8") as output_file:
            for line in file:
                dict={}
                data = json.loads(line)
                ST_NOP_CANIn=data["ST_NOP_CANIn"]
                path=data["path"]
                ACU1_Driver_Buckle=ST_NOP_CANIn["ACU1_Driver_Buckle"]
                IDB7_HDCAtive=ST_NOP_CANIn["IDB7_HDCAtive"]
                Region_A_SwError=ST_NOP_CANIn["Region_A_SwError"]



                dict["ACU1_Driver_Buckle"]=ACU1_Driver_Buckle
                dict["IDB7_HDCAtive"] = IDB7_HDCAtive
                dict["Region_A_SwError"] = Region_A_SwError

                # 记录发生的时间：
                header_=ST_NOP_CANIn["header_"]
                secs=header_["secs"]
                nsecs = header_["nsecs"]
                start_time_str=get_time_str(secs)
                dict["secs"] = secs
                dict["start_time_str"] = start_time_str
                dict["path"] = path
                dict["nsecs"] = int(nsecs)


                json_string = json.dumps(dict,indent=None,ensure_ascii=False)
                # 写入处理后的结果到文件
                output_file.write(json_string + '\n')

def read_can_out(upload_can_dir, dict_can_dir):
    with open(upload_can_dir, 'r',encoding="utf-8") as file:
        with open(dict_can_dir, 'w',encoding="utf-8") as output_file:
            for line in file:
                dict={}
                data = json.loads(line)
                ST_NOP_CANOut=data["ST_NOP_CANOut"]
                path = data["path"]
                ADCS8_NPilot_SysState = ST_NOP_CANOut["ADCS8_NPilot_SysState"]
                ADCS2_longitudCtrlType= ST_NOP_CANOut["ADCS2_longitudCtrlType"]
                ADCS2_ADAS_EPSLateralCtrlType= ST_NOP_CANOut["ADCS2_ADAS_EPSLateralCtrlType"]
                ADCS12_longitudDisableInfo=ST_NOP_CANOut["ADCS12_longitudDisableInfo"]
                ADCS8_NNPSysState=ST_NOP_CANOut["ADCS8_NNPSysState"]
                ADCS12_NPilotSysInfo=ST_NOP_CANOut["ADCS12_NPilotSysInfo"]
                ADCS12_NNP_State_Reminder = ST_NOP_CANOut["ADCS12_NNP_State_Reminder"]
                ADCS12_P2N_State_Reminder = ST_NOP_CANOut["ADCS12_P2N_State_Reminder"]
                NNP_out_reserved8= ST_NOP_CANOut["NNP_out_reserved8"]
                ADCS12_NNP_RINO = ST_NOP_CANOut["ADCS12_NNP_RINO"]


                dict["ADCS8_NPilot_SysState"]=ADCS8_NPilot_SysState
                dict["ADCS2_longitudCtrlType"] = ADCS2_longitudCtrlType
                dict["ADCS2_ADAS_EPSLateralCtrlType"] = ADCS2_ADAS_EPSLateralCtrlType
                dict["ADCS12_longitudDisableInfo"] = ADCS12_longitudDisableInfo
                dict["ADCS12_NPilotSysInfo"] = ADCS12_NPilotSysInfo
                dict["ADCS8_NNPSysState"] = ADCS8_NNPSysState
                dict["ADCS12_NNP_State_Reminder"] = ADCS12_NNP_State_Reminder
                dict["ADCS12_P2N_State_Reminder"] = ADCS12_P2N_State_Reminder
                dict["NNP_out_reserved8"] = NNP_out_reserved8
                dict["ADCS12_NNP_RINO"] = ADCS12_NNP_RINO

                # 记录发生的时间：
                header_=ST_NOP_CANOut["header_"]
                secs=header_["secs"]
                nsecs = header_["nsecs"]
                start_time_str=get_time_str(secs)
                dict["secs"] = secs
                dict["start_time_str"] = start_time_str
                dict["path"] = path
                dict["nsecs"] = int(nsecs)

                json_string = json.dumps(dict, indent=None,ensure_ascii=False)
                # 写入处理后的结果到文件
                output_file.write(json_string + '\n')

def read_upload_20ms(source_dir,target_path):
    with open(source_dir, 'r',encoding="utf-8") as file:
        with open(target_path, 'w',encoding="utf-8") as output_file:
            for line in file:
                result_dict={}
                data = json.loads(line)
                ST_Upload_20ms_t = data["ST_Upload_20ms_t"]
                path = data["path"]

                XCP_SignalGroup_5 = ST_Upload_20ms_t["XCP_SignalGroup_5"]
                SuppressReason=XCP_SignalGroup_5["SuppressReason"]


                header_=ST_Upload_20ms_t["header_"]
                nsecs = header_["nsecs"]
                result_dict["nsecs"] = int(nsecs)
                result_dict["SuppressReason"] = int(SuppressReason)
                result_dict["path"] =path

                XCP_SignalGroup_5 = ST_Upload_20ms_t["XCP_SignalGroup_5"]
                VLCAccOOIData_AccOOINextLong_Attributes_uiObjectID = XCP_SignalGroup_5["VLCAccOOIData_AccOOINextLong_Attributes_uiObjectID"]
                EnvmGenObjectList_aObject_0_Kinematic_fAabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_0_Kinematic_fAabsX"]
                EnvmGenObjectList_aObject_0_Kinematic_fAabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_0_Kinematic_fAabsY"]
                EnvmGenObjectList_aObject_0_Kinematic_fDistX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_0_Kinematic_fDistX"]
                EnvmGenObjectList_aObject_0_Kinematic_fDistY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_0_Kinematic_fDistY"]
                EnvmGenObjectList_aObject_0_Kinematic_fVabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_0_Kinematic_fVabsX"]
                EnvmGenObjectList_aObject_0_Kinematic_fVabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_0_Kinematic_fVabsY"]
                EnvmGenObjectList_aObject_1_Kinematic_fAabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_1_Kinematic_fAabsX"]
                EnvmGenObjectList_aObject_1_Kinematic_fAabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_1_Kinematic_fAabsY"]
                EnvmGenObjectList_aObject_1_Kinematic_fDistX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_1_Kinematic_fDistX"]
                EnvmGenObjectList_aObject_1_Kinematic_fDistY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_1_Kinematic_fDistY"]
                EnvmGenObjectList_aObject_1_Kinematic_fVabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_1_Kinematic_fVabsX"]
                EnvmGenObjectList_aObject_1_Kinematic_fVabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_1_Kinematic_fVabsY"]
                EnvmGenObjectList_aObject_2_Kinematic_fAabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_2_Kinematic_fAabsX"]
                EnvmGenObjectList_aObject_2_Kinematic_fAabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_2_Kinematic_fAabsY"]
                EnvmGenObjectList_aObject_2_Kinematic_fDistX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_2_Kinematic_fDistX"]
                EnvmGenObjectList_aObject_2_Kinematic_fDistY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_2_Kinematic_fDistY"]
                EnvmGenObjectList_aObject_2_Kinematic_fVabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_2_Kinematic_fVabsX"]
                EnvmGenObjectList_aObject_2_Kinematic_fVabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_2_Kinematic_fVabsY"]
                EnvmGenObjectList_aObject_3_Kinematic_fAabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_3_Kinematic_fAabsX"]
                EnvmGenObjectList_aObject_3_Kinematic_fAabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_3_Kinematic_fAabsY"]
                EnvmGenObjectList_aObject_3_Kinematic_fDistX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_3_Kinematic_fDistX"]
                EnvmGenObjectList_aObject_3_Kinematic_fDistY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_3_Kinematic_fDistY"]
                EnvmGenObjectList_aObject_3_Kinematic_fVabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_3_Kinematic_fVabsX"]
                EnvmGenObjectList_aObject_3_Kinematic_fVabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_3_Kinematic_fVabsY"]
                EnvmGenObjectList_aObject_4_Kinematic_fAabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_4_Kinematic_fAabsX"]
                EnvmGenObjectList_aObject_4_Kinematic_fAabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_4_Kinematic_fAabsY"]
                EnvmGenObjectList_aObject_4_Kinematic_fDistX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_4_Kinematic_fDistX"]
                EnvmGenObjectList_aObject_4_Kinematic_fDistY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_4_Kinematic_fDistY"]
                EnvmGenObjectList_aObject_4_Kinematic_fVabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_4_Kinematic_fVabsX"]
                EnvmGenObjectList_aObject_4_Kinematic_fVabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_4_Kinematic_fVabsY"]
                EnvmGenObjectList_aObject_5_Kinematic_fAabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_5_Kinematic_fAabsX"]
                EnvmGenObjectList_aObject_5_Kinematic_fAabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_5_Kinematic_fAabsY"]
                EnvmGenObjectList_aObject_5_Kinematic_fDistX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_5_Kinematic_fDistX"]
                EnvmGenObjectList_aObject_5_Kinematic_fDistY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_5_Kinematic_fDistY"]
                EnvmGenObjectList_aObject_5_Kinematic_fVabsX = XCP_SignalGroup_5["EnvmGenObjectList_aObject_5_Kinematic_fVabsX"]
                EnvmGenObjectList_aObject_5_Kinematic_fVabsY = XCP_SignalGroup_5["EnvmGenObjectList_aObject_5_Kinematic_fVabsY"]

                result_dict["VLCAccOOIData_AccOOINextLong_Attributes_uiObjectID"] = VLCAccOOIData_AccOOINextLong_Attributes_uiObjectID
                result_dict["EnvmGenObjectList_aObject_0_Kinematic_fAabsX"] = EnvmGenObjectList_aObject_0_Kinematic_fAabsX
                result_dict["EnvmGenObjectList_aObject_0_Kinematic_fAabsY"] = EnvmGenObjectList_aObject_0_Kinematic_fAabsY
                result_dict["EnvmGenObjectList_aObject_0_Kinematic_fDistX"] = EnvmGenObjectList_aObject_0_Kinematic_fDistX
                result_dict["EnvmGenObjectList_aObject_0_Kinematic_fDistY"] = EnvmGenObjectList_aObject_0_Kinematic_fDistY
                result_dict["EnvmGenObjectList_aObject_0_Kinematic_fVabsX"] = EnvmGenObjectList_aObject_0_Kinematic_fVabsX
                result_dict["EnvmGenObjectList_aObject_0_Kinematic_fVabsY"] = EnvmGenObjectList_aObject_0_Kinematic_fVabsY
                result_dict["EnvmGenObjectList_aObject_1_Kinematic_fAabsX"] = EnvmGenObjectList_aObject_1_Kinematic_fAabsX
                result_dict["EnvmGenObjectList_aObject_1_Kinematic_fAabsY"] = EnvmGenObjectList_aObject_1_Kinematic_fAabsY
                result_dict["EnvmGenObjectList_aObject_1_Kinematic_fDistX"] = EnvmGenObjectList_aObject_1_Kinematic_fDistX
                result_dict["EnvmGenObjectList_aObject_1_Kinematic_fDistY"] = EnvmGenObjectList_aObject_1_Kinematic_fDistY
                result_dict["EnvmGenObjectList_aObject_1_Kinematic_fVabsX"] = EnvmGenObjectList_aObject_1_Kinematic_fVabsX
                result_dict["EnvmGenObjectList_aObject_1_Kinematic_fVabsY"] = EnvmGenObjectList_aObject_1_Kinematic_fVabsY
                result_dict["EnvmGenObjectList_aObject_2_Kinematic_fAabsX"] = EnvmGenObjectList_aObject_2_Kinematic_fAabsX
                result_dict["EnvmGenObjectList_aObject_2_Kinematic_fAabsY"] = EnvmGenObjectList_aObject_2_Kinematic_fAabsY
                result_dict["EnvmGenObjectList_aObject_2_Kinematic_fDistX"] = EnvmGenObjectList_aObject_2_Kinematic_fDistX
                result_dict["EnvmGenObjectList_aObject_2_Kinematic_fDistY"] = EnvmGenObjectList_aObject_2_Kinematic_fDistY
                result_dict["EnvmGenObjectList_aObject_2_Kinematic_fVabsX"] = EnvmGenObjectList_aObject_2_Kinematic_fVabsX
                result_dict["EnvmGenObjectList_aObject_2_Kinematic_fVabsY"] = EnvmGenObjectList_aObject_2_Kinematic_fVabsY
                result_dict["EnvmGenObjectList_aObject_3_Kinematic_fAabsX"] = EnvmGenObjectList_aObject_3_Kinematic_fAabsX
                result_dict["EnvmGenObjectList_aObject_3_Kinematic_fAabsY"] = EnvmGenObjectList_aObject_3_Kinematic_fAabsY
                result_dict["EnvmGenObjectList_aObject_3_Kinematic_fDistX"] = EnvmGenObjectList_aObject_3_Kinematic_fDistX
                result_dict["EnvmGenObjectList_aObject_3_Kinematic_fDistY"] = EnvmGenObjectList_aObject_3_Kinematic_fDistY
                result_dict["EnvmGenObjectList_aObject_3_Kinematic_fVabsX"] = EnvmGenObjectList_aObject_3_Kinematic_fVabsX
                result_dict["EnvmGenObjectList_aObject_3_Kinematic_fVabsY"] = EnvmGenObjectList_aObject_3_Kinematic_fVabsY
                result_dict["EnvmGenObjectList_aObject_4_Kinematic_fAabsX"] = EnvmGenObjectList_aObject_4_Kinematic_fAabsX
                result_dict["EnvmGenObjectList_aObject_4_Kinematic_fAabsY"] = EnvmGenObjectList_aObject_4_Kinematic_fAabsY
                result_dict["EnvmGenObjectList_aObject_4_Kinematic_fDistX"] = EnvmGenObjectList_aObject_4_Kinematic_fDistX
                result_dict["EnvmGenObjectList_aObject_4_Kinematic_fDistY"] = EnvmGenObjectList_aObject_4_Kinematic_fDistY
                result_dict["EnvmGenObjectList_aObject_4_Kinematic_fVabsX"] = EnvmGenObjectList_aObject_4_Kinematic_fVabsX
                result_dict["EnvmGenObjectList_aObject_4_Kinematic_fVabsY"] = EnvmGenObjectList_aObject_4_Kinematic_fVabsY
                result_dict["EnvmGenObjectList_aObject_5_Kinematic_fAabsX"] = EnvmGenObjectList_aObject_5_Kinematic_fAabsX
                result_dict["EnvmGenObjectList_aObject_5_Kinematic_fAabsY"] = EnvmGenObjectList_aObject_5_Kinematic_fAabsY
                result_dict["EnvmGenObjectList_aObject_5_Kinematic_fDistX"] = EnvmGenObjectList_aObject_5_Kinematic_fDistX
                result_dict["EnvmGenObjectList_aObject_5_Kinematic_fDistY"] = EnvmGenObjectList_aObject_5_Kinematic_fDistY
                result_dict["EnvmGenObjectList_aObject_5_Kinematic_fVabsX"] = EnvmGenObjectList_aObject_5_Kinematic_fVabsX
                result_dict["EnvmGenObjectList_aObject_5_Kinematic_fVabsY"] = EnvmGenObjectList_aObject_5_Kinematic_fVabsY

                #时间格式化：
                Tbox_TimeYHDHMS = str(ST_Upload_20ms_t["Tbox_TimeYHDHMS"])
                start_time_str = Tbox_TimeYHDHMS[0:4] + "-" + Tbox_TimeYHDHMS[4:6] + "-" + Tbox_TimeYHDHMS[ 6:8] + " " + Tbox_TimeYHDHMS[8:10] + ":" + Tbox_TimeYHDHMS[10:12] + ":" + Tbox_TimeYHDHMS[12:14]
                result_dict["start_time_str"] = start_time_str




                json_string = json.dumps(result_dict,  indent=None,ensure_ascii=False)
                # 写入处理后的结果到文件
                output_file.write(json_string + '\n')
def read_upload_100ms(source_dir,target_path):
    with open(source_dir, 'r',encoding="utf-8") as file:
        with open(target_path, 'w',encoding="utf-8") as output_file:
            for line in file:
                result_dict={}
                data = json.loads(line)
                ST_Upload_100ms_t = data["ST_Upload_100ms_t"]
                path = data["path"]



                TriggerSignals = ST_Upload_100ms_t["TriggerSignals"]
                ADCS8_ACCState = TriggerSignals["ADCS8_ACCState"]
                ADCS12_longitudDisableInfo = TriggerSignals["ADCS12_longitudDisableInfo"]
                ADCS2_AEBPartialBrake = TriggerSignals["ADCS2_AEBPartialBrake"]
                ADCS2_AEBFullBrake = TriggerSignals["ADCS2_AEBFullBrake"]

                # 新增字段：
                IDB1_BrakePedalApplied=TriggerSignals["IDB1_BrakePedalApplied"]
                IDB1_BrakePedalAppliedV = TriggerSignals["IDB1_BrakePedalAppliedV"]


                ACU2_LongAccSensorValue= TriggerSignals["ACU2_LongAccSensorValue"]
                ACU2_LatAccSensorValue= TriggerSignals["ACU2_LatAccSensorValue"]
                CS1_GearPositionReqSt= TriggerSignals["CS1_GearPositionReqSt"]

                ADCS12_NPilotSysInfo= TriggerSignals["ADCS12_NpilotSysInfo"]
                ADCS8_NPilot_SysState= TriggerSignals["ADCS8_NPilot_SysState"]
                ADCS8_NNPSysState= TriggerSignals["ADCS8_NNPSysState"]
                ACU2_VehicleDynYawRate= TriggerSignals["ACU2_VehicleDynYawRate"]
                EPS1_TorsionBarTorque= TriggerSignals["EPS1_TorsionBarTorque"]
                EPS1_SteerAngleSpd=TriggerSignals["EPS1_SteerAngleSpd"]
                IDB3_VehicleSpd=TriggerSignals["IDB3_VehicleSpd"]
                ICU2_Odometer=TriggerSignals["ICU2_Odometer"]
                ADCS8_FCWStatus=TriggerSignals["ADCS8_FCWStatus"]
                ADCS8_lateralCtrtakeove=TriggerSignals["ADCS8_lateralCtrtakeove"]
                ADCS8_longitudCtrlTakeOverReq=TriggerSignals["ADCS8_longitudCtrlTakeOverReq"]


                result_dict["ADCS8_lateralCtrtakeove"] = ADCS8_lateralCtrtakeove
                result_dict["ADCS8_longitudCtrlTakeOverReq"] = ADCS8_longitudCtrlTakeOverReq



                XCP_SignalGroup_1= ST_Upload_100ms_t["XCP_SignalGroup_1"]
                VLCCDHypotheses_Hypothesis_0_fVrelX=XCP_SignalGroup_1["VLCCDHypotheses_Hypothesis_0_fVrelX"]
                VLCCDHypotheses_Hypothesis_0_eEBAObjectClass=XCP_SignalGroup_1["VLCCDHypotheses_Hypothesis_0_eEBAObjectClass"]
                ADCS12_longitudCtrlSysInfo=TriggerSignals["ADCS12_longitudCtrlSysInfo"]
                VLCCDHypotheses_Hypothesis_0_fVrelY=XCP_SignalGroup_1["VLCCDHypotheses_Hypothesis_0_fVrelY"]
                AccDisplayObj_CONTROL_ACCEL=XCP_SignalGroup_1["AccDisplayObj_CONTROL_ACCEL"]
                VLCCDHypotheses_Hypothesis_0_fDistY = XCP_SignalGroup_1["VLCCDHypotheses_Hypothesis_0_fDistY"]
                VLCCDHypotheses_Hypothesis_0_fDistX = XCP_SignalGroup_1["VLCCDHypotheses_Hypothesis_0_fDistX"]
                VLCCDHypotheses_Hypothesis_0_fTTC = XCP_SignalGroup_1["VLCCDHypotheses_Hypothesis_0_fTTC"]

                result_dict["AccDisplayObj_CONTROL_ACCEL"] =AccDisplayObj_CONTROL_ACCEL
                result_dict["VLCCDHypotheses_Hypothesis_0_fDistY"] = VLCCDHypotheses_Hypothesis_0_fDistY
                result_dict["VLCCDHypotheses_Hypothesis_0_fDistX"] = VLCCDHypotheses_Hypothesis_0_fDistX
                result_dict["VLCCDHypotheses_Hypothesis_0_fTTC"] = VLCCDHypotheses_Hypothesis_0_fTTC


                XCP_SignalGroup_2 = ST_Upload_100ms_t["XCP_SignalGroup_2"]
                CamLaneData_CourseInfo_2_CourseInfoSegNear_f_Length = XCP_SignalGroup_2["CamLaneData_CourseInfo_2_CourseInfoSegNear_f_Length"]
                g_LCFSEN_proDebugs_sTJASADebug_TJALKA_LaneCenterInvalid_btf = XCP_SignalGroup_2["g_LCFSEN_proDebugs_sTJASADebug_TJALKA_LaneCenterInvalid_btf"]
                CamLaneData_LaneMarkerInfo_1_f_MarkerDist = XCP_SignalGroup_2["CamLaneData_LaneMarkerInfo_1_f_MarkerDist"]
                CamLaneData_LaneMarkerInfo_2_f_MarkerDist = XCP_SignalGroup_2["CamLaneData_LaneMarkerInfo_2_f_MarkerDist"]
                CamLaneData_CourseInfo_1_CourseInfoSegNear_f_Length= XCP_SignalGroup_2["CamLaneData_CourseInfo_1_CourseInfoSegNear_f_Length"]
                g_LCFSEN_proDebugs_sTJASADebug_TJALKA_WeakReady_bool= XCP_SignalGroup_2["g_LCFSEN_proDebugs_sTJASADebug_TJALKA_WeakReady_bool"]
                g_LCF_TJASA_Output_TJASTM_SysStateTJA_nu= XCP_SignalGroup_2["g_LCF_TJASA_Output_TJASTM_SysStateTJA_nu"]
                g_LCFSEN_proDebugs_sTJASADebug_TJALKA_StrongReady_bool= XCP_SignalGroup_2["g_LCFSEN_proDebugs_sTJASADebug_TJALKA_StrongReady_bool"]
                CamLaneData_CourseInfo_1_CourseInfoSegNear_f_C0= XCP_SignalGroup_2["CamLaneData_CourseInfo_1_CourseInfoSegNear_f_C0"]


                result_dict["CamLaneData_CourseInfo_2_CourseInfoSegNear_f_Length"] = CamLaneData_CourseInfo_2_CourseInfoSegNear_f_Length
                result_dict["g_LCFSEN_proDebugs_sTJASADebug_TJALKA_LaneCenterInvalid_btf"] = g_LCFSEN_proDebugs_sTJASADebug_TJALKA_LaneCenterInvalid_btf
                result_dict["CamLaneData_LaneMarkerInfo_1_f_MarkerDist"] = CamLaneData_LaneMarkerInfo_1_f_MarkerDist
                result_dict["CamLaneData_LaneMarkerInfo_2_f_MarkerDist"] = CamLaneData_LaneMarkerInfo_2_f_MarkerDist
                result_dict["CamLaneData_CourseInfo_1_CourseInfoSegNear_f_Length"] = CamLaneData_CourseInfo_1_CourseInfoSegNear_f_Length
                result_dict["g_LCFSEN_proDebugs_sTJASADebug_TJALKA_WeakReady_bool"] = g_LCFSEN_proDebugs_sTJASADebug_TJALKA_WeakReady_bool
                result_dict["g_LCF_TJASA_Output_TJASTM_SysStateTJA_nu"] = g_LCF_TJASA_Output_TJASTM_SysStateTJA_nu
                result_dict["g_LCFSEN_proDebugs_sTJASADebug_TJALKA_StrongReady_bool"] = g_LCFSEN_proDebugs_sTJASADebug_TJALKA_StrongReady_bool
                result_dict["CamLaneData_CourseInfo_1_CourseInfoSegNear_f_C0"] = CamLaneData_CourseInfo_1_CourseInfoSegNear_f_C0

                header_=ST_Upload_100ms_t["header_"]
                nsecs = header_["nsecs"]
                result_dict["ADCS8_FCWStatus"] = ADCS8_FCWStatus
                result_dict["VLCCDHypotheses_Hypothesis_0_fVrelY"] = VLCCDHypotheses_Hypothesis_0_fVrelY
                result_dict["nsecs"] = int(nsecs)
                result_dict["ADCS8_ACCState"]=ADCS8_ACCState
                result_dict["ADCS12_longitudDisableInfo"] = ADCS12_longitudDisableInfo
                result_dict["ADCS2_AEBPartialBrake"] = ADCS2_AEBPartialBrake
                result_dict["ADCS2_AEBFullBrake"] = ADCS2_AEBFullBrake
                result_dict["IDB1_BrakePedalApplied"] = IDB1_BrakePedalApplied
                result_dict["IDB1_BrakePedalAppliedV"] = IDB1_BrakePedalAppliedV
                result_dict["ADCS12_NPilotSysInfo"] = ADCS12_NPilotSysInfo
                result_dict["ADCS8_NPilot_SysState"] = ADCS8_NPilot_SysState
                result_dict["ADCS8_NNPSysState"] = ADCS8_NNPSysState
                result_dict["ACU2_VehicleDynYawRate"] =ACU2_VehicleDynYawRate
                result_dict["EPS1_TorsionBarTorque"] = EPS1_TorsionBarTorque
                result_dict["EPS1_SteerAngleSpd"] = EPS1_SteerAngleSpd
                result_dict["IDB3_VehicleSpd"] = IDB3_VehicleSpd
                result_dict["ICU2_Odometer"] = ICU2_Odometer
                result_dict["VLCCDHypotheses_Hypothesis_0_fVrelY"] = VLCCDHypotheses_Hypothesis_0_fVrelY
                result_dict["VLCCDHypotheses_Hypothesis_0_fVrelX"] = VLCCDHypotheses_Hypothesis_0_fVrelX
                result_dict["VLCCDHypotheses_Hypothesis_0_eEBAObjectClass"] = VLCCDHypotheses_Hypothesis_0_eEBAObjectClass
                result_dict["path"]=path
                result_dict["ADCS12_longitudCtrlSysInfo"] = ADCS12_longitudCtrlSysInfo
                result_dict["ACU2_LongAccSensorValue"] = ACU2_LongAccSensorValue
                result_dict["ACU2_LatAccSensorValue"] = ACU2_LatAccSensorValue
                result_dict["CS1_GearPositionReqSt"] = CS1_GearPositionReqSt

                #时间格式化：
                Tbox_TimeYHDHMS = str(ST_Upload_100ms_t["Tbox_TimeYHDHMS"])
                start_time_str = Tbox_TimeYHDHMS[0:4] + "-" + Tbox_TimeYHDHMS[4:6] + "-" + Tbox_TimeYHDHMS[ 6:8] + " " + Tbox_TimeYHDHMS[8:10] + ":" + Tbox_TimeYHDHMS[10:12] + ":" + Tbox_TimeYHDHMS[12:14]
                result_dict["start_time_str"] = start_time_str

                json_string = json.dumps(result_dict, indent=None,ensure_ascii=False)
                # 写入处理后的结果到文件
                output_file.write(json_string + '\n')
def save_20ms(source_100ms_dir,upload_100ms_file,dict_100ms_file,savepath,object_key):
    upload_100ms_dir = os.path.dirname(upload_100ms_file)
    dict_100ms_dir = os.path.dirname(dict_100ms_file)
    path_exsist(upload_100ms_dir)
    path_exsist(dict_100ms_dir)
    path_exsist(savepath)
    write_2_local(source_100ms_dir, upload_100ms_file,object_key)
    read_upload_20ms(upload_100ms_file, dict_100ms_file)
    df_20ms = pd.read_json(dict_100ms_file, lines=True)

    if df_20ms.shape[0] > 0:
        print(savepath + 'st_20ms'+'.pkl')
        df_20ms.to_pickle(savepath + 'st_20ms'+'.pkl')

    return df_20ms
def save_100ms(source_100ms_dir,upload_100ms_file,dict_100ms_file,savepath,object_key):
    upload_100ms_dir = os.path.dirname(upload_100ms_file)
    dict_100ms_dir = os.path.dirname(dict_100ms_file)
    path_exsist(upload_100ms_dir)
    path_exsist(dict_100ms_dir)
    path_exsist(savepath)
    write_2_local(source_100ms_dir, upload_100ms_file,object_key)
    read_upload_100ms(upload_100ms_file, dict_100ms_file)
    df_100ms = pd.read_json(dict_100ms_file, lines=True)

    if df_100ms.shape[0] > 0:
        print(savepath + 'st_100ms'+'.pkl')
        df_100ms.to_pickle(savepath + 'st_100ms'+'.pkl')

    return  df_100ms


def save_can_in(source_can_dir, upload_can_in_file, dict_can_in_file,savepath,object_key):
    upload_can_in_dir = os.path.dirname(upload_can_in_file)
    dict_can_in_dir = os.path.dirname(dict_can_in_file)
    path_exsist(upload_can_in_dir)
    path_exsist(dict_can_in_dir)
    path_exsist(savepath)

    print("upload_can_in_dir: "+upload_can_in_dir)
    write_2_local(source_can_dir, upload_can_in_file,object_key)
    read_can_in(upload_can_in_file, dict_can_in_file)
    df_can_in = pd.read_json(dict_can_in_file, lines=True)

    if df_can_in.shape[0] > 0:
        print(savepath + 'st_can_in'+'.pkl')
        df_can_in.to_pickle(savepath + 'st_can_in'+'.pkl')

    return df_can_in

def save_can_out(source_can_dir, upload_can_out_file, dict_can_out_file,savepath,object_key):
    upload_can_out_dir = os.path.dirname(upload_can_out_file)
    dict_can_out_dir = os.path.dirname(dict_can_out_file)
    path_exsist(upload_can_out_dir)
    path_exsist(dict_can_out_dir)
    path_exsist(savepath)

    write_2_local(source_can_dir, upload_can_out_file,object_key)
    read_can_out(upload_can_out_file, dict_can_out_file)
    df_can_out = pd.read_json(dict_can_out_file, lines=True)

    if df_can_out.shape[0] > 0:
        print(savepath + 'st_can_out'+'.pkl')
        df_can_out.to_pickle(savepath + 'st_can_out'+'.pkl')

    return df_can_out


def get_data_task(object_key,bucket_name):
    # 使用split函数将字符串分割成部分
    parts = object_key.split('-')
    # 提取最后一部分
    last_part = parts[-1]
    # 可以选择去掉文件扩展名（.zip）
    bagid = last_part.split('.')[0]

    if "3200" not in bagid and "3201" not in bagid:
        arr = object_key.split("/")
        vechicle_id = arr[3]
        daystr = arr[4]
        hourstr = arr[5]
        filename = arr[6]
        filearr = filename.split("-")
        file_prefix = filearr[0]

        localpath = "/data/code/input/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix
        file_name = "/data/code/input/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/" + filename
        download(bucket_name, object_key, file_name, localpath)
        unzip(file_name)
        # bag_dir="./test/fcm3979bc77u0nv1c3ghg-EP40_TDA4_drive_sc-20231223-160031-3308/asw/StUpload/"
        bag_dir = localpath + "/asw/StUpload"
        # 列举所有的bag 包
        file_list = list_files_in_directory(bag_dir)
        # 对bag包进行解密
        data_jiemi(file_list, vechicle_id, daystr, hourstr)
        # 列举所有的json文件：
        # /data/code/input/v2305175911/20231225/19/fcm4mhvrc77u0nv1hhbqg/3308/aggregator/
        json_file = "/data/code/input/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/source_json/"
        json_file_list = list_files_in_directory(json_file)
        print("json_file_list: " + str(json_file_list))

        for jsonfile in json_file_list:
            # /data/code/input/v2305175911/20231225/19/fcm4mhvrc77u0nv1hhbqg/source_json/3307/aggregator/json/ST_NOP_CANIn.json
            # json_name: ST_NOP_CANIn.json
            json_name = jsonfile.split("/json/")[1]
            # json_dir: /data/code/input/v2305175911/20231225/19/fcm4mhvrc77u0nv1hhbqg/source_json/3307/aggregator
            json_dir = jsonfile.split("/json/")[0]
            type_file=json_dir.split("/")[len(json_dir.split("/"))-1]

            if json_name in "ST_Upload_100ms_t.json":
                print("ST_Upload_100ms_t.json")
                upload_100ms_dir = json_dir + "/upload/ST_Upload_100ms_t.json"
                dict_100ms_dir = json_dir + "/dict/ST_Upload_100ms_t.json"
                savepath = json_dir + "/pk/"
                save_100ms(jsonfile, upload_100ms_dir, dict_100ms_dir, savepath, object_key)
            elif json_name in "ST_NOP_CANIn.json":
                print("ST_NOP_CANIn.json")
                upload_can_in_file = json_dir + "/upload/ST_NOP_CANIn.json"
                dict_can_in_file = json_dir + "/dict/ST_NOP_CANIn.json"
                savepath = json_dir + "/pk/"
                save_can_in(jsonfile, upload_can_in_file, dict_can_in_file, savepath, object_key)

            elif json_name in "ST_NOP_CANOut.json":
                upload_can_out_file = json_dir + "/upload/ST_NOP_CANOut.json"
                dict_can_out_file = json_dir + "/dict/ST_NOP_CANOut.json"
                savepath = json_dir + "/pk/"
                save_can_out(jsonfile, upload_can_out_file, dict_can_out_file, savepath, object_key)
            elif json_name in "ST_Upload_20ms_t.json":
                upload_can_out_file = json_dir + "/upload/ST_Upload_20ms_t.json"
                dict_can_out_file = json_dir + "/dict/ST_Upload_20ms_t.json"
                savepath = json_dir + "/pk/"
                save_20ms(jsonfile, upload_can_out_file, dict_can_out_file, savepath, object_key)

        print("执行后续的任务。。。。。。。。。。。。")
        os.system('python3.9 /data/code/data_label.py  ' + object_key)


if __name__ == '__main__':
    bucket_name="advc-data-upload-2100095994-cn-beijing"
    # object_key = "EP40/TDA4/SC/v2212839244/20240102/19/fcm9v8brc77ub4tg6u5mg-EP40_TDA4_drive_sc-20240102-193151-2300.zip"
    # object_key="EP40/TDA4/SC/v2212702578/20240102/18/fcm9uaj3c77ue5pn2ftpg-EP40_TDA4_drive_sc-20240102-182822-2301.zip"
    # object_key = "EP40/TDA4/SC/v2302015306/20240104/11/fcmb1vurc77ubf214tu50-EP40_TDA4_drive_sc-20240104-110320-2302.zip"
    # object_key = "EP40/TDA4/SC/v2302015306/20240109/20/fcmek013c77u76vkb2580-EP40_TDA4_drive_sc-20240109-204552-2303.zip"
    # object_key = "EP40/TDA4/SC/v2309656921/20240109/17/fcmegrojc77ubf21i6lv0-EP40_TDA4_drive_sc-20240109-171158-2304.zip"
    # object_key = "EP40/TDA4/SC/v2212027854/20240113/14/fcmh2q8bc77ue5pnv57pg-EP40_TDA4_drive_sc-20240113-142632-2309.zip"
    object_key = "EP40/TDA4/SC/v2310621861/20240103/16/fcmahd43c77u0nv27v3r0-EP36_TDA4_drive_sc-20240103-161052-2311.zip"
    # object_key = "EP40/TDA4/SC/v2212489695/20240110/14/fcmf3c7bc77ue5pnmlc3g-EP40_TDA4_drive_sc-20240110-141552-2314.zip"
    # object_key = "EP40/TDA4/SC/v2212702578/20240101/13/fcm94vcbc77u0nv22bqgg-EP40_TDA4_drive_sc-20240101-133750-3308.zip"


    # object_key = "EP40/TDA4/SC/v2212839244/20240107/22/fcmdbcsbc77u76vk61ba0-EP40_TDA4_drive_sc-20240107-223420-3308.zip"
    # object_key = "EP40/TDA4/SC/v2301921435/20240106/0/fcmc37kbc77u0nv2dsj70-EP40_TDA4_drive_sc-20240106-005226-2300.zip"

    # object_key = os.environ.get('BAG_PATH')
    get_data_task(object_key,bucket_name)














